Attribute VB_Name = "Database"
Option Explicit
Public Function getConectionString()
    getConectionString = "DSN=daclidb"
End Function
Public Function getValue(record As ADODB.Recordset, value As String) As String
    Dim result As String
    result = ""
    'if record.State
    If record.State = 0 Then Exit Function
    If Not record.EOF Then
        If Len(record(value).value) > 0 Then result = record(value).value
    End If
    getValue = result
End Function
Public Function conect(ByRef ConectionDB As ADODB.Connection)
    If ConectionDB.State = adStateOpen Then Exit Function
    ConectionDB.Open getConectionString
End Function
Public Function closeDb(ByRef ConectionDB As ADODB.Connection)
    If ConectionDB.State = adStateClosed Then Exit Function
    ConectionDB.Close
End Function
Function getNewNumber(GuidId As String, Field As String, ByVal Tipo As String) As Long
    Dim conexion As New ADODB.Connection
    Dim tNextNumber As New ADODB.Recordset
    Dim nextNumber As Long
    nextNumber = 1
    conect conexion
    tNextNumber.Open "SELECT MAX(" & Field & ") AS maximo FROM next_number WHERE tipo='" & Tipo & "'", conexion
    If (Not IsNull(tNextNumber.Fields("maximo").value)) Then nextNumber = tNextNumber.Fields("maximo").value + 1
    conexion.Execute "INSERT INTO next_number(guid_id," & Field & ",tipo) VALUES('" & GuidId & "'," & nextNumber & ",'" & Tipo & "')"
    getNewNumber = nextNumber
    conexion.Close
End Function

